package com.gitee.zeric.codec.generaters;

import cn.hutool.core.util.StrUtil;
import com.gitee.zeric.codec.exception.ParamErrorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zj
 * @date 2021/12/24
 */
@Slf4j
public class CodeGeneraterFactory {

    /**
     * 系统内置实现类
     */
    private static ConcurrentHashMap<String, CodeGenerater> generaterMap = new ConcurrentHashMap<>(16);

    public static CodeGenerater getGenerater(String implementClass, StringRedisTemplate stringRedisTemplate) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        if (StrUtil.isBlank(implementClass)) {
            return null;
        }
        if (generaterMap.contains(implementClass)) {
            return generaterMap.get(implementClass);
        } else {
            CodeGenerater codeGenerater;
            if (StrUtil.equals("com.gitee.zeric.codec.generaters.WaterGenerater", implementClass)) {
                codeGenerater = new WaterGenerater(stringRedisTemplate);
            } else {
                codeGenerater = (AbstractCodeGerator) Class.forName(implementClass).newInstance();
            }
            generaterMap.put(implementClass, codeGenerater);
            return codeGenerater;
        }
    }
}
